package com.ycsj.goldmedalnewconcept.teacher.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.view.scroll.MyListView;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.TkKeys;
import com.ycsj.goldmedalnewconcept.common.bean.SubListBean;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.ReadChooseAnswerAdapter;
import com.ycsj.goldmedalnewconcept.teacher.adapter.ReadShowAnswerAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.ReadLvInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 单选题
 * Created by Administrator on 2018/9/3 0003.
 */

public class SingleChoiceFragment extends BaseLazyFragment {

    //页面数据对象
    private TkItemBean pageInfo;

    //单选题的数据对象
    private SubListBean subListBean;

    //学生做题0  1学生查看解析 2老师查看题和解析-有人做(全校正确率和小题正确率都显示) 3(习题分析)老师查看题和解析-没人做,对应查看单题
    private String type;

    //题目类型
    private String tvTitleType;

    //题目类型
    private TextView tvType;
    /**
     * 选择答案
     */
    private TextView tv_content;

    //作答的整体布局
    private LinearLayout ll_user_zd;
    private MyListView lv;


    //展示你的作答,正确答案,每个选项的正确率以及题目解析的相关视图
    //该视图只有在已完成状态下才显示出来
    private LinearLayout llShowAnswer;
    //你的答案
    private TextView tv_own_answer;
    //正确答案
    private TextView tv_right_answer;
    //展示选项正确率的ListView
    private MyListView lv_show_answer;
    //解析
    private TextView tv_parse_answer;

    //全校正确率的布局
    private LinearLayout llQxzql;

    //全校正确率的TextView
    private TextView tvQxzql;

    //已完成状态下的解析和你的作答
    private String analysis;

    //用户的作答
    private String ownAnser;

    //该单选题的正确答案的标号
    private String rightAnswerIndex;

    private ReadChooseAnswerAdapter answerAdapter;

    private List<ReadLvInfo> oldDatas;

    //当前大题在整个题目中所处的位置
    private int currPos;

    public static final SingleChoiceFragment newInstance(TkItemBean item, String type,int currPos) {
        SingleChoiceFragment teacherGoldFragment = new SingleChoiceFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("type", type);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tk_single_choice;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {

        //获取传递的对象
        pageInfo = (TkItemBean) getArguments().getSerializable("data");
        type = getArguments().getString("type");

        //获取当前大题在整个题目中所处的位置
        currPos = getArguments().getInt("currpos");

        //单选题只有一个题型,取第0个元素
        if(pageInfo!=null){
            if(pageInfo.getSub_list()!=null){
                subListBean = pageInfo.getSub_list().get(0);
            }
        }

        //获取题目的解析
        analysis = pageInfo.getAnalysis();
        if (TextUtils.isEmpty(analysis)) {
            analysis = "略";
        }

        //用户的作答
        if (pageInfo.getSub_list().get(0).getStu_option() != null) {
            ownAnser = pageInfo.getSub_list().get(0).getStu_option();
        } else {
            ownAnser = "";
        }

    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvType = view.findViewById(R.id.tv_type);
        tv_content = view.findViewById(R.id.tv_content);
        lv = view.findViewById(R.id.lv);
        if (null != pageInfo) {
            tv_content.setText(pageInfo.getContent());
        }
        llShowAnswer = view.findViewById(R.id.ll_show_answer);
        tv_own_answer = view.findViewById(R.id.tv_own_answer);
        tv_right_answer = view.findViewById(R.id.tv_right_answer);
        lv_show_answer = view.findViewById(R.id.lv_show_answer);
        tv_parse_answer = view.findViewById(R.id.tv_parse_answer);
        ll_user_zd = view.findViewById(R.id.ll_user_zd);
        llQxzql = view.findViewById(R.id.ll_qxzql);
        tvQxzql = view.findViewById(R.id.tv_qxzql);

        //设置数据
        initLv();

        if ("0".equals(type)) {
            //学生未做题
            llShowAnswer.setVisibility(View.GONE);


            if("0".equals(AccountUtils.getRole(getActivity()))){
                //未完成列表选项可以点击
                lv.setEnabled(true);
            }else{
                //家长不可点击
                lv.setEnabled(false);
            }
        } else if ("1".equals(type)) {
            //学生查看解析
            llShowAnswer.setVisibility(View.VISIBLE);
            lv.setEnabled(false);

            //作答布局显示
            ll_user_zd.setVisibility(View.VISIBLE);

            //全校正确率和小题正确率显示
            llQxzql.setVisibility(View.GONE);
            lv_show_answer.setVisibility(View.GONE);

            initHasCompetedData();

        } else if ("2".equals(type)) {
            //查看单题或老师查看学生(有人做)
            llShowAnswer.setVisibility(View.VISIBLE);
            lv.setEnabled(false);

            //作答布局不显示
            ll_user_zd.setVisibility(View.GONE);

            //全校正确率和小题正确率显示
            llQxzql.setVisibility(View.VISIBLE);
            lv_show_answer.setVisibility(View.VISIBLE);

            initHasCompetedData();
        } else if ("3".equals(type)) {
            //查看单题或老师查看学生(没人做)
            llShowAnswer.setVisibility(View.VISIBLE);
            lv.setEnabled(false);

            //作答布局不显示
            ll_user_zd.setVisibility(View.GONE);

            //全校正确率和小题正确率不显示
            llQxzql.setVisibility(View.GONE);
            lv_show_answer.setVisibility(View.GONE);

            initHasCompetedData();

        }



        if (TextUtils.isEmpty(pageInfo.getN_topic_name())) {
            tvTitleType = pageInfo.getN_type_name();
        } else {
            tvTitleType = pageInfo.getN_type_name() + "-" + pageInfo.getN_topic_name();
        }
        //设置题目类型
        tvType.setText(tvTitleType);
    }


    /**
     * 设置提供选择ListView的数据,如果是已完成的状态则ListView不可点击,同时数据模型会改变
     * 可操作可点击
     */
    private void initLv() {

        if (subListBean != null) {
            //获取该题答案的选项,此处格式是ABCD序号
            rightAnswerIndex = subListBean.getOption();

            //创建需要展示的数据集合
            oldDatas = new ArrayList<>();


            //配置每个选项的正确率
            SubListBean.ProBean pro = subListBean.getPro();
            //获取选项
            List<String> listBeans = subListBean.getList();


            if (null != listBeans && listBeans.size() != 0) {
                for (int i = 0; i < listBeans.size(); i++) {

                    if(!TextUtils.isEmpty(listBeans.get(i))){


                    ReadLvInfo info = new ReadLvInfo();

                    //设置编号
                    info.questionIndex = letters[i];

                    //设置答案内容
                    info.questionContent = listBeans.get(i);

                    //给每个选项配置正确答案标号
                    if (TextUtils.isEmpty(rightAnswerIndex)) {
                        info.rightAnswer = "";
                    } else {

                        info.rightAnswer = rightAnswerIndex;

                    }

                    //已完成情况下给每个选项配置用户选择的答案以及正确答案
                    //配置用户选择的答案
                    info.chooseAnswer = ownAnser;

                    //给每个选项配置正确答案标号
                    info.rightAnswer = rightAnswerIndex;

                    if("2".equals(type)){


                    //配置没个选项在正确率
                    if (pro != null) {
                        if (!TextUtils.isEmpty(pro.getA())) {
                            if (i == 0) {
                                info.probability = pro.getA();
                            }
                        }
                        if (!TextUtils.isEmpty(pro.getB())) {
                            if (i == 1) {
                                info.probability = pro.getB();
                            }
                        }
                        if (!TextUtils.isEmpty(pro.getC())) {
                            if (i == 2) {
                                info.probability = pro.getC();
                            }
                        }
                        if (!TextUtils.isEmpty(pro.getD())) {
                            if (i == 3) {
                                info.probability = pro.getD();
                            }
                        }
                    } else {
                        info.probability = "100.0%";
                    }
                    }
                    oldDatas.add(info);
                }
                }
            }

            //设置适配器
            answerAdapter = new ReadChooseAnswerAdapter(Integer.parseInt(type), getContext(), oldDatas, R.layout.tk_lv_read_choose_answer);
            lv.setAdapter(answerAdapter);

            if ("0".equals(type)) {
                //学生未答题
                //设置条目点击事件
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

                        if (oldDatas.get(pos).isSelected) {
                            return;
                        } else {
                            oldDatas.get(pos).isSelected = true;
                            //同步数据状态
                            answerAdapter.upDateStete(pos);
                            //保存选择的答案
                            //通过EventBs将用户作答本小题的相关信息传递给TkMainActivity保存
                            //currPos对应的每个大题的小题的位置,单选题默认就一个小题
                            int choosePos = 0;
                            //是否如正确
                            boolean chooseResult = false;
                            if (oldDatas.get(pos).rightAnswer.equals(oldDatas.get(pos).questionIndex)) {
                                //用户作答正确
                                chooseResult = true;
                            } else {
                                //用户作答错误
                                chooseResult = false;
                            }

                            BusInfo busInfo = new BusInfo();
                            busInfo.tkMsg = TkKeys.TKMsg;
                            busInfo.tkChoosePos = choosePos;
                            busInfo.tkChooseAnswer = oldDatas.get(pos).questionIndex;

                            busInfo.tkChooseResult = chooseResult;
                            EventBus.getDefault().post(busInfo);
                        }
                    }
                });

            }
        }
    }


    /**
     * 初始化你的作答,正确答案,答案正确率和解析
     * 需要通过用户是否是已完成和未完成的状态来确定该部分的视图显示与隐藏,同时设置数据的数据源不一样
     * 该设置需要取已完成状态的数据源集合
     */
    private void initHasCompetedData() {
        if ("1".equals(type) || "2".equals(type)) {
            //学生查看解析和老师查看学生解析(有人做),显示全校正确率和选项正确率
            ReadShowAnswerAdapter answerAdapter = new ReadShowAnswerAdapter(getContext(), oldDatas, R.layout.tk_lv_read_show_answer);
            lv_show_answer.setAdapter(answerAdapter);

            if ("1".equals(type)) {
                //学生查看解析的时候显示自己的作答
                tv_own_answer.setText(ownAnser);
                if (ownAnser.equals(rightAnswerIndex)) {
                    tv_own_answer.setBackgroundResource(R.drawable.tk_tv_right_answer);
                } else {
                    tv_own_answer.setBackgroundResource(R.drawable.tk_tv_own_choose_answer);
                }

            }

        }

        if ("2".equals(type)) {
            //全校正确率
            if (null != subListBean) {
                tvQxzql.setText(subListBean.getYes_pro());
            }
        }
        //正确答案
        tv_right_answer.setText(rightAnswerIndex);

        //设置解析
        tv_parse_answer.setText(analysis.trim().toString());
    }
}

